package com.trkj.service.wdm;

import java.sql.Timestamp;
import java.util.Date;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Sort.Direction;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.trkj.dao.wdm.SocialRelationsDAO;
import com.trkj.pojo.Edubackground;
import com.trkj.pojo.SocialRelation;
import com.trkj.service.ServiceException;

@Service
@Transactional(rollbackFor =Exception.class)
public class SocialRelationsService {
	@Autowired
	private SocialRelationsDAO socialrelationsdao;
	/**
	 * 分页查询所有
	 * @return
	 */
	public List<SocialRelation> queryByPage(int page,int rows){
		return  socialrelationsdao.queryByNaPage(new PageRequest(page-1, rows,Direction.DESC,"sociCreatordate"));
	}
	/**
	 * 获取总记录条数
	 * @return
	 */
	public long queryByPageCount(){
		return socialrelationsdao.count();
	}
	
	/**
	 * 新增
	 */
	public void insert(String sociName,String sociPerson){
		try {
			SocialRelation rea = new SocialRelation();
			rea.setSociName(sociName);
			rea.setSociPerson(sociPerson);
			Date date = new Date();  
			rea.setSociCreatordate(new Timestamp(date.getTime()));
			rea.setSociUpdatedate(new Timestamp(date.getTime()));
			socialrelationsdao.save(rea);
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			throw new ServiceException(e);
		}
	}
	/**
	 * 编辑
	 */
	public void update(Long sociId,String sociName,String sociPerson){
		try {
			SocialRelation rea = socialrelationsdao.findOne(sociId);
			rea.setSociName(sociName);
			rea.setSociPerson(sociPerson);
			rea.setSociUpdatedate(new Timestamp(new Date().getTime()));
			socialrelationsdao.save(rea);
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			throw new ServiceException(e);
		}
		
	}
	
	
	
	/**
	 * 删除
	 */
	public void delete(Long id){
		try {
			socialrelationsdao.delete(id);
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			throw new ServiceException(e);
		}
	}
	/**
	 * 加载
	 * @return
	 */
	public SocialRelation loadReasonsLeaving(Long sociId) {
		
		return socialrelationsdao.findOne(sociId);
	}
	/**
	 * 模糊查询
	 * @param text
	 * @param page
	 * @param rows
	 * @return
	 */
	public List<SocialRelation> selectForid_name(String text,int page,int rows){
		try {
			return socialrelationsdao.query_By_idorname("%"+text+"%",new PageRequest(page-1, rows));
		} catch (Exception e) {
			e.printStackTrace();
			throw new ServiceException(e);
		}
	}
	
	/**
	 * 获取总记录条数
	 * @return
	 */
	public long count(String text){
		return socialrelationsdao.count("%"+text+"%");
	}
}
